import { eventHandler } from 'h3';
import { verifyAccessToken } from '~/utils/jwt-utils';
import {
  unAuthorizedResponse,
  useResponseError,
  useResponseSuccess,
} from '~/utils/response';

export default eventHandler((event) => {
  console.log('User info API called');
  try {
    const userinfo = verifyAccessToken(event);
    console.log('User info result:', userinfo);
    if (!userinfo) {
      console.log('No user info found, returning unauthorized');
      return unAuthorizedResponse(event);
    }
    console.log('Returning user info:', userinfo);
    return useResponseSuccess(userinfo);
  } catch (error) {
    console.error('User info API error:', error);
    return useResponseError('InternalServerError', 'Failed to get user info');
  }
});
